package com.mateng.treasurechest.presenter.recipe

import com.mateng.treasurechest.model.recipe.service.IRecipeService
import com.mateng.treasurechest.presenter.BasePresenter
import com.mateng.treasurechest.view.recipe.RECIPE_KEY
import com.mateng.treasurechest.view.recipe.interfaces.IRecipeCategoryView
import com.mateng.treasurechest.view.recipe.interfaces.IRecipeDetailsView
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers

/**
 * Created by mateng on 2018/1/26.
 */

class RecipePresenter : BasePresenter() {

    var iRecipeCategoryView: IRecipeCategoryView? = null

    var iRecipeDetailsView: IRecipeDetailsView? = null
    /**
     * 获取菜谱分类标签
     */
    fun getRecipeCategory() {
        registerSubscription(IRecipeService.instance.getCategory(RECIPE_KEY)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe({ t ->
                    iRecipeCategoryView?.getRecipeCategoryDataSuccess(t)
                }) { err: Throwable ->
                    iRecipeCategoryView?.requestError(err)
                })
    }

    /**
     * 按标签查询菜谱
     */
    fun getMenuSearch(cid: String, page: Int, size: Int) {
        registerSubscription(IRecipeService.instance.getMenuSearch(RECIPE_KEY, cid, "", page, size)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe({ t ->
                    iRecipeCategoryView?.getMenuSearchDataSuccess(t)
                }) { err: Throwable ->
                    iRecipeCategoryView?.requestError(err)
                })
    }


    /**
     * 按id查询做菜详情
     */
    fun getRecipeDetails(id: String) {
        registerSubscription(IRecipeService.instance.getRecipeDetails(RECIPE_KEY, id)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe({ t ->
                    iRecipeDetailsView?.getRecipeDetailsDataSuccess(t)
                }) { err: Throwable ->
                    iRecipeDetailsView?.requestError(err)
                })
    }
}
